package cn.scw.project.common;

import cn.scw.common.constant.Constants;
import cn.scw.common.constant.SysConsts;
import cn.scw.common.separate.ServiceNumberService;
import cn.scw.common.separate.model.MessageTemplateModel;
import cn.scw.common.utils.DateUtils;
import cn.scw.common.utils.IdUtils;
import cn.scw.common.utils.StringUtils;
import cn.scw.common.utils.VerifyCodeUtils;
import cn.scw.common.utils.sign.Base64;
import cn.scw.framework.redis.RedisCache;
import cn.scw.framework.web.domain.AjaxResult;
import cn.scw.framework.web.domain.server.Sys;
import cn.scw.project.facility.domain.FacFacility;
import cn.scw.project.separate.domain.SaUser;
import cn.scw.project.separate.service.SaUserService;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import cn.scw.project.facility.service.IFacFacilityService;
import org.springframework.web.servlet.ModelAndView;

/**
 * 打印机广告
 *
 * @author api
 */
@Controller
@RequestMapping(SysConsts.CONTEXT_PATH)
public class PrinterController {

    @Autowired
    private IFacFacilityService iFacFacilityService;

    @Resource
    private ServiceNumberService serviceNumberService;

    @Resource
    private SaUserService saUserService;

    @RequestMapping("/printer")
    public ModelAndView printer(HttpServletRequest request, String deviceId) {
        ModelAndView modelAndView = new ModelAndView("printer.html");

//        if(deviceId != null) {
//            FacFacility fac = iFacFacilityService.selectFacFacilityByCode(deviceId);
//            if(fac != null) {
//                String qr_url = fac.getQrCodeUrl();
//                //request.setAttribute("qr_url",qr_url);
//                modelAndView.addObject("qr_url", qr_url);
//
//                System.out.println("printer qr_url=" + qr_url);
//            }else{
//                System.out.println("printer fac is null");
//            }
//        }else{
//            System.out.println("printer deviceId is null");
//        }
//        //request.setAttribute("deviceId",deviceId);
//        modelAndView.addObject("deviceId",deviceId);

//        return "printer.html";

        return modelAndView;
    }

    /**
     * 公众号授权获取openid
     *
     * @param code
     */
    @RequestMapping("/oauth")
    public String oauth(String code, Model model) throws IOException {
        if (StringUtils.isBlank(code)) {
            return "redirect:" + serviceNumberService.getOauthUrl();
        }
        String openId = serviceNumberService.getOpenId(code);
        if (StringUtils.isBlank(openId)) model.addAttribute("errMsg", "授权失败");
        else {
            SaUser saUser = saUserService.selectOpenId(openId);
            model.addAttribute("saUser", saUser);
        }
        model.addAttribute("openId", openId);
        return "bindAccount";
    }

    /**
     * 公众号授权获取openid
     */
    @RequestMapping("/testSend")
    public void testSend(String openId) throws IOException {

        MessageTemplateModel templateModel = new MessageTemplateModel();
        templateModel.setFirst(MessageTemplateModel.Model.init("您有一笔新的交易到账"));
        templateModel.setKeyword1(MessageTemplateModel.Model.init("100.00"));
        templateModel.setKeyword2(MessageTemplateModel.Model.init("微信支付"));
        templateModel.setKeyword3(MessageTemplateModel.Model.init("TS00010001"));
        templateModel.setKeyword4(MessageTemplateModel.Model.init(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss")));
        templateModel.setRemark(MessageTemplateModel.Model.init("感谢你的使用。"));

        serviceNumberService.sendTemplate("orPHjwnXdxcpqlwQOSo7WQaSrr5U", templateModel);
    }
}
